package testcase;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import static until.Sign.getSign;

public class OppoHttpClientPost {
    @Test
    public void testName() throws IOException {
        // 获取连接客户端工具
        DefaultHttpClient httpClient = new DefaultHttpClient();

        String entityStr = null;
        HttpResponse response = null;

        // 创建POST请求对象
        HttpPost httpPost = new HttpPost("http://116.204.7.166:8018/v1/c2s/device/rename");

        /*
         * 添加请求参数
         */
        // 创建请求参数

//        //添加除了json格式以外的任何参数
        List<NameValuePair> list = new LinkedList<NameValuePair>();
        BasicNameValuePair param1 = new BasicNameValuePair("deviceId", "1099511823961");
        BasicNameValuePair param2 = new BasicNameValuePair("name", "23");
        list.add(param1);
        list.add(param2);
        // 使用URL实体转换工具
        UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
        httpPost.setEntity(entityParam);

        // 获取签名
//        HashMap<String, String> signMap = new HashMap<String, String>();
//        signMap.put("appId","11115992");
//        signMap.put("token","TOKEN_hagP1+PNns3R0UOa4DNPgsy8f7Hknf+fNveHdBb9CCTfNKX4dJJeWEMqy+huprqU");
//        signMap.put("timestamp", String.valueOf(System.currentTimeMillis()));
//        String secret_key="$3@%$$#"; //加密secret
//        System.out.println("得到签名sign1:"+getSign(signMap,secret_key));

        /*
         * 添加请求头信息
         */
        // 浏览器表示
        httpPost.addHeader("User-Agent", "3.8.0)");
        // 传输的类型
        httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");
        httpPost.addHeader("appId","com.oppo.ohome");
        httpPost.addHeader("token","TOKEN_hagP1+PNns3R0UOa4DNPgsy8f7Hknf+fNveHdBb9CCTfNKX4dJJeWEMqy+huprqU");
        httpPost.addHeader("signature","xI2UyuCp96XesrzU3q0rf9awQkgrtjV3lxGP5fdgCa8");
        httpPost.addHeader("timestamp", String.valueOf(System.currentTimeMillis()));
      //  httpPost.addHeader("signature",getSign(signMap,secret_key));
//        httpPost.addHeader("nonce","3");

        // 执行请求
        response = httpClient.execute(httpPost);
        // 获得响应的实体对象
        HttpEntity entity = response.getEntity();
        // 使用Apache提供的工具类进行转换成字符串
        entityStr = EntityUtils.toString(entity, "UTF-8");

        // System.out.println(Arrays.toString(response.getAllHeaders()));

        // 打印响应内容
        System.out.println(entityStr);

    }


}
